package com.xingchi.tornado.security.config;

import com.xingchi.tornado.security.filter.UnifiedAuthenticationFilter;
import com.xingchi.tornado.security.handler.LoginPostProcessor;
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
import org.springframework.security.config.annotation.web.configurers.AbstractAuthenticationFilterConfigurer;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

import java.util.List;

/**
 * 统一认证登录配置
 *
 * @author xingchi
 * @date 2025/3/9 21:19
 */
public class UnifiedLoginConfigurer<H extends HttpSecurityBuilder<H>> extends
        AbstractAuthenticationFilterConfigurer<H, UnifiedLoginConfigurer<H>, UnifiedAuthenticationFilter> {

    /**
     * Creates a new instance
     *
     * @param authenticationFilter      the {@link AbstractAuthenticationProcessingFilter} to
     *                                  use
     * @param defaultLoginProcessingUrl the default URL to use for
     *                                  {@link #loginProcessingUrl(String)}
     */
    protected UnifiedLoginConfigurer(UnifiedAuthenticationFilter unifiedAuthenticationFilter) {
        super(unifiedAuthenticationFilter, null);
    }

    @Override
    protected RequestMatcher createLoginProcessingUrlMatcher(String loginProcessingUrl) {
        return new AntPathRequestMatcher(loginProcessingUrl);
    }

    public UnifiedLoginConfigurer<H> loginPostProcessors(List<LoginPostProcessor<Authentication>> loginPostProcessors) {
        this.getAuthenticationFilter().setLoginPostProcessors(loginPostProcessors);
        return this;
    }

}
